<?php
namespace app\index\controller;
use app\index\controller\Common;
use app\index\model\Teacher as TeacherModel;
/**
* 教师风采管理
*/
class Teacher extends Common
{
	
	public function index(){
		$teacherRes=db('teacher')->order('tid desc')->paginate(10);
		$this->assign('teacherRes',$teacherRes);
		return  $this->fetch();
	}
	public function add(){
		if (request()->isPost()) {
			$data=input('post.');
			$teacher=new TeacherModel();
			 $validate = \think\Loader::validate('Teacher');
            if(!$validate->scene('add')->check($data)){
                $this->error($validate->getError());
            }
            if ($teacher->save($data)) {
                $this->success('添加教师风采成功！',url('index'));
            }else{
                $this->error('添加教师风采失败！');
            }

			return;
		}
		return $this->fetch();
	}
	public function edit(){
		if (request()->isPost()) {
			$data=input('post.');
			$teacher=new TeacherModel();
            $validate = \think\Loader::validate('Teacher');
            if(!$validate->scene('edit')->check($data)){
                $this->error($validate->getError());
            }
            if ($teacher->update($data)) {
                $this->success('修改教师风采成功！',url('index'));
            }else{
                $this->error('修改教师风采失败！');
            }
            return;
		}
		$tid=input('tid');
		$teacherRes=db('teacher')->where(array('tid'=>$tid))->find();
		$this->assign('teacherRes',$teacherRes);
		return $this->fetch();
	}
	public function del(){
		if(TeacherModel::destroy(input('tid'))){
            $this->success('删除教师风采成功！',url('index'));
        }else{
            $this->error('删除教师风采失败！');
        }
	}
}